<?php

/**
 * This class converts any string into a image (gif / jpg/ png) on the fly.
 *
 * @author  Rochak Chauhan
 *
 * @version 2: Added new features to display and/or save created image. 
 *				
 * Dependency - GD Library Needed
 */

require_once('WebSafeColors.inc.php');

class ConvertTextToImage extends WebSafeColors {
	private $text_color = 'black';
	private $image;
	private $width;
	private $height;
	
	/**
	 * Function to Initialise the image
	 *
	 * @param $width  int   - required width of the image
	 * @param $height  int - required height of the image
	 * @param $bgColor  string - Color (in hexadecimal / RGB / Color Name)
	 * @param $textColor  string - Color (in hexadecimal / RGB / Color Name)
	 *
	 */	
	public function initializeImage($width, $height, $bgColor, $textColor) {
		if($width <= 0 || $height <= 0 || !is_numeric($width) || !is_numeric($height) )  {
			die("<BR><font color='red' size='3' face='verdana'><b>ERROR:</b> Invalid Width and/or height.   </font>");			
		}
		$this->width = $width;
		$this->height = $height;
		$this->image = imagecreate($width, $height) or die("Cannot Initialize new GD image stream");
		$bgColorArray = explode(",", parent::getRgbValue($bgColor));
		$textColorArray = explode(",", parent::getRgbValue($textColor));

		imagecolorallocate($this->image, $bgColorArray[0], $bgColorArray[1], $bgColorArray[2]);
		$this->text_color = imagecolorallocate($this->image, $textColorArray[0], $textColorArray[1], $textColorArray[2]);
	}
	
	/**
	 * Function to create a PNG Image of the provided text 
	 *
	 * @param $fontSize int - Size of the text
	 * @param $x int - x-Coordinate of the starting of the text (image)
	 * @param $y int - y-Coordinate of the starting of the text (image)
	 * @param $textContent string - string to br converted into PNG
	 *
	 */
	public function ShowTextAsPng($fontSize, $x, $y, $textContent){
		header("Content-type: image/png");
		
		imagestring($this->image, $fontSize, $x, $y,  $textContent, $this->text_color);
		imagepng($this->image);
	}
	
	/**
	 * Function to create a JPG Image of the provided text
	 *
	 * @param $fontSize int - Size of the text
	 * @param $x int - x-Coordinate of the starting of the text (image)
	 * @param $y int - y-Coordinate of the starting of the text (image)
	 * @param $textContent string - string to br converted into JPG
	 *
	 */
	public function ShowTextAsJpg($fontSize, $x, $y, $textContent){
		header("Content-type: image/jpg");
		
		imagestring($this->image, $fontSize, $x, $y,  $textContent, $this->text_color);
		imagejpeg($this->image);
	}
	
	
	/**
	 * Function to create a GIF Image of the provided text 
	 *
	 * @param $fontSize int - Size of the text
	 * @param $x int - x-Coordinate of the starting of the text (image)
	 * @param $y int - y-Coordinate of the starting of the text (image)
	 * @param $textContent string - string to br converted into GIF
	 *
	 */
	public function ShowTextAsGif($fontSize, $x, $y, $textContent){
		header("Content-type: image/gif");
		
		imagestring($this->image, $fontSize, $x, $y,  $textContent, $this->text_color);
		imagegif($this->image);
	}

	/**
	 * Function to save the created GIF Image of the provided text 
	 *
	 * @param $fontSize int - Size of the text
	 * @param $x int - x-Coordinate of the starting of the text (image)
	 * @param $y int - y-Coordinate of the starting of the text (image)
	 * @param $textContent string - string to br converted into GIF
 	 * @param [$fileName] - string optional name of the file (by default: image) 
	 * PS: Do NOT add Extention in the file name
	 *	   Image will be stored in the directory containing this class.
	 *
	 * @return boolean - true if image is created , false otherwise
 	 */
	public function SaveTextAsGif($fontSize, $x, $y, $textContent, $fileName='image'){
		imagestring($this->image, $fontSize, $x, $y,  $textContent, $this->text_color);
		return imagegif($this->image, $fileName.".gif");
	}
	
	/**
	 * Function to save the created JPG Image of the provided text 
	 *
	 * @param $fontSize int - Size of the text
	 * @param $x int - x-Coordinate of the starting of the text (image)
	 * @param $y int - y-Coordinate of the starting of the text (image)
	 * @param $textContent string - string to br converted into GIF
 	 * @param [$fileName] - string optional name of the file (by default: image) 
	 * PS: Do NOT add Extention in the file name
	 *	   Image will be stored in the directory containing this class.
	 *
	 * @return boolean - true if image is created , false otherwise
 	 */
	public function SaveTextAsJpg($fontSize, $x, $y, $textContent, $fileName='image'){
		imagestring($this->image, $fontSize, $x, $y,  $textContent, $this->text_color);
		return imagegif($this->image, $fileName.".jpg");
	}

	/**
	 * Function to save the created PNG Image of the provided text 
	 *
	 * @param $fontSize int - Size of the text
	 * @param $x int - x-Coordinate of the starting of the text (image)
	 * @param $y int - y-Coordinate of the starting of the text (image)
	 * @param $textContent string - string to br converted into GIF
 	 * @param [$fileName] - string optional name of the file (by default: image) 
	 * PS: Do NOT add Extention in the file name
	 *	   Image will be stored in the directory containing this class.
	 *
	 * @return boolean - true if image is created , false otherwise
 	 */
	public function SaveTextAsPng($fontSize, $x, $y, $textContent, $fileName='image'){
		imagestring($this->image, $fontSize, $x, $y,  $textContent, $this->text_color);
		return imagegif($this->image, $fileName.".png");
	}

} 
?>